<?php

namespace app\shop\model;
use app\common\model\BaseModel;
class ShopGoods extends BaseModel
{
    public function specs()
    {
        return $this->hasMany(ShopGoodsSpec::class, 'gid')->where('is_deleted', 0);
    }

    protected function getIsCollectAttr($val,$data){
        return ShopCollect::uid()->where('good_id',$data['id'])->count() ? true : false;
    }
    public function cart(){
        return $this->hasMany(ShopCart::class,'good_id');
    }
    protected function getImageAttr($val){
       $val= explode('|',$val);
       return $this->getImgUrl($val);
    }
    public function cate(){
        return $this->belongsTo('ShopGoodsCate','cate_id');
    }
    protected function getAdvImgAttr($val){
        return $this->getImgUrl($val);
    }
    protected function getLogoAttr($val){
        return $this->getImgUrl($val);
    }
    protected function getTagsIdAttr($val){
        return explode('.',$val);
    }
    protected function setTagsIdAttr($val){
        return implode('.',$val);
    }
    protected function getSaleCountAttr($val,$data){
       return  ShopGoodsSpec::where('gid',$data['id'])->where('is_deleted', 0)->sum('sale');
    }

    public function orderGood(){
        return $this->hasMany(ShopOrderGoods::class,'good_id');
    }

}